package cn.sym.system.service.impl;

import cn.sym.system.domain.Role;
import cn.sym.system.mapper.RoleMapper;
import cn.sym.system.service.IRoleService;
import cn.sym.basic.service.impl.BaseServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author sym
 * @since 2022-02-20
 */
@Service
@Transactional
public class RoleServiceImpl extends BaseServiceImpl<Role> implements IRoleService {
    @Autowired
    private RoleMapper roleMapper;

    @Override
    public void add(Role role) {
        super.add(role);
        System.out.println(role);
        System.out.println(role.getId());
        initRelationTable(role);
    }
    private void initRelationTable(Role role) {
        if (role.getId()!=null){
            roleMapper.romveRoleMenus(role.getId());
            roleMapper.romveRolePermission(role.getId());
        }
        if (role.getMenus()!=null){
            Map<String,Object> map = new HashMap<>();
            map.put("roleId",role.getId());
            map.put("menuIds",role.getMenus());
            roleMapper.addMenus(map);
        }
        if (role.getPermissions()!=null){
            Map<String,Object> map = new HashMap<>();
            map.put("roleId",role.getId());
            map.put("perIds",role.getPermissions());
            roleMapper.addPermissions(map);
        }
    }

    @Override
    public void update(Role role) {
        super.update(role);
    }
}
